package com.yunsc.product.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yunsc.product.dto.GoodsSkuDTO;
import com.yunsc.product.entity.YunscGoodsSkuEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 商品规格表
 * 
 * @author adan
 * @email 2028038953@qq.com
 * @date 2024-04-04 21:28:42
 */
@Mapper
public interface YunscGoodsSkuDao extends BaseMapper<YunscGoodsSkuEntity> {

    List<GoodsSkuDTO> selectByGoodsId(@Param("goodsId") Integer goodsId);


    @Update("update yunsc_goods_sku set stock_num = yunsc_goods_sku.stock_num - #{stockNum}\n" +
            "                        where goods_id = #{goodsId} and goods_sku_id = #{goodsSkuId}\n" +
            "and yunsc_goods_sku.stock_num > #{stockNum}")
    int reduceGoodsSkuStockNum(@Param("goodsId")Integer goodsId,
                               @Param("goodsSkuId") String goodsSkuId,
                               @Param("stockNum") Integer stockNum);

    /**
     * 增加商品规格库存
     * @param goodsId 商品id
     * @param goodsSkuId 商品sku id
     * @param totalNum 增加的库存数量
     */
    @Update("update yunsc_goods_sku set stock_num = yunsc_goods_sku.stock_num + #{totalNum}\n" +
            "                        where goods_id = #{goodsId} and goods_sku_id = #{goodsSkuId}")
    void increaseGoodsSkuStockNum(@Param("goodsId")Integer goodsId,
                                  @Param("goodsSkuId") String goodsSkuId,
                                  @Param("totalNum") Integer totalNum);
}
